package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.MarketRole;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.vo.role.PermissionListVO;
import com.cskaoyan.bean.vo.role.RoleOptionsVO;
import com.cskaoyan.bean.vo.role.UpdatePermissionPO;
import com.cskaoyan.service.admin.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Bowen Deng
 * @since 2022/06/27 15:56
 */
@RestController
@RequestMapping("admin/role")
public class AdminRoleController {

    @Autowired
    AdminRoleService adminRoleService;

    /**
     * admin/role/option请求的显示所有role的方法
     *
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Bowen Deng
     * @since 2022/06/27 3:58 下午
     */
    @RequestMapping("options")
    public BaseRespVo options() {
        CommonData<RoleOptionsVO> roleOptionsVOCommonData = adminRoleService.listRoleOptions();
        return BaseRespVo.ok(roleOptionsVOCommonData);
    }

    /**
     * admin/role/list请求对应的显示所有role的方法
     *
     * @param info
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Bowen Deng
     * @since 2022/06/27 7:34 下午
     */
    @RequestMapping("list")
    public BaseRespVo list(String name, BasePageInfo info) {
        CommonData<MarketRole> roleCommonData = adminRoleService.listAllRoles(name, info);
        return BaseRespVo.ok(roleCommonData);
    }

    /**
     * 添加role的请求，前端传过来的参数不用判空，也没有什么命名要求，所以只需要查询与数据库里的name是否重复
     * 重复返回100，插入成功返回1，插入失败返回0，数据库错误
     *
     * @param role
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Bowen Deng
     * @since 2022/06/27 8:00 下午
     */
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody MarketRole role) {
        Integer code = adminRoleService.addRole(role);
        if (code == 100) {
            BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
            baseRespVo.setErrno(602);
            baseRespVo.setErrmsg("角色已经存在");
            return baseRespVo;
        }
        MarketRole marketRole = adminRoleService.selectRoleByName(role.getName());
        return BaseRespVo.ok(marketRole);
    }

    /**
     * 更新role的请求，前端传过来的参数不用判空，也没有什么命名要求，所以只需要查询与数据库里的name是否重复
     * 重复返回100，更新成功返回1，更新失败返回0，数据库错误
     *
     * @param role
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Bowen Deng
     * @since 2022/06/27 8:21 下午
     */
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketRole role) {
        Integer code = adminRoleService.updateRole(role);
        if (code == 100) {
            BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
            baseRespVo.setErrno(602);
            baseRespVo.setErrmsg("角色已经存在");
            return baseRespVo;
        }
        return BaseRespVo.ok();
    }

    /**
     * 删除role
     * 删除应该加判断，是否能删除
     * 数据库表中deleted列0不能删除，1可以删除
     *
     * @param role
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Bowen Deng
     * @since 2022/06/27 8:27 下午
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketRole role) {
        if(role.getId()==1 ||role.getId()==2 || role.getId()==3){
            BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
            baseRespVo.setErrno(604);
            baseRespVo.setErrmsg("该角色无法删除");
            return baseRespVo;
        }
        Integer code = adminRoleService.deleteRole(role);
        return BaseRespVo.ok();
    }

    /**
     * 根据roldID显示这个role已经有的权限，同时还要显示所有的权限
     *
     * @param roleId
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Bowen Deng
     * @since 2022/06/27 11:26 下午
     */
    @GetMapping("permissions")
    public BaseRespVo permissions(String roleId) {
        PermissionListVO permissionListVO = adminRoleService.getPermissions(roleId);
        return BaseRespVo.ok(permissionListVO);
    }

    /**
     * 更新
     * market_permission表
     *
     * @param update
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Bowen Deng
     * @since 2022/06/28 9:02 上午
     */
    @PostMapping("permissions")
    public BaseRespVo updatePermissions(@RequestBody UpdatePermissionPO update) {
        if(update.getRoleId()==1 || update.getRoleId()==2 || update.getRoleId()==3){
            BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
            baseRespVo.setErrno(604);
            baseRespVo.setErrmsg("该角色无法修改权限");
            return baseRespVo;
        }
        Integer code = adminRoleService.updatePermissions(update);
        return BaseRespVo.ok();
    }
}
